package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.TrainingResult;
import com.think.teaching.service.impl.TrainingResultServiceImpl;
import com.think.teaching.service.impl.TrainingServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "实训结果管理接口")
public class TrainingResultController {
	
	private final TrainingResultServiceImpl trainingResultService;
	
	private final TrainingServiceImpl trainingServiceImpl;

	public TrainingResultController(TrainingResultServiceImpl trainingResultService, TrainingServiceImpl trainingServiceImpl) {
		this.trainingResultService = trainingResultService;
		this.trainingServiceImpl = trainingServiceImpl;
	}

	@ApiOperation("添加新的实训结果")
	@PostMapping("/trainingResult")
	public Result AddNewTrainingResult(@RequestBody()TrainingResult data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingResult trainingResult = trainingResultService.AddTrainingResult(data);
			if (trainingResult != null) {
				return Result.success(trainingResult);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新实训结果")
	@RequestMapping(value = "/trainingResult",method = RequestMethod.PUT)
	public Result UpdateTrainingResult(@RequestBody()TrainingResult data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingResult trainingResult = trainingResultService.UpdateTrainingResult(data);
			if (trainingResult != null) {
				return Result.success(trainingResult);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除实训结果")
	@RequestMapping(value = "admin/trainingResult/{id}",method = RequestMethod.DELETE)
	public Result DeleteTrainingResult(@PathVariable("id")Integer id) {
		TrainingResult trainingResult = trainingResultService.getById(id);
		if (trainingResult == null) {
			return Result.failure(ResultCode.TRAINING_RESULT_NOT_EXIST);
		} else {
			if (trainingResultService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取实训结果详情")
	@RequestMapping(value = "/trainingResult/{id}",method = RequestMethod.GET)
	public Result GetTrainingResult(@PathVariable("id")Integer id) {
		TrainingResult trainingResult = trainingResultService.getById(id);
		if (trainingResult == null) {
			return Result.failure(ResultCode.TRAINING_RESULT_NOT_EXIST);
		} else {

			return Result.success(trainingResult);
		}
	}
	
	@ApiOperation("管理员修改实训结果状态")
	@RequestMapping(value = "admin/trainingResult/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateTrainingResultState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		TrainingResult trainingResult = trainingResultService.getById(id);
		if (trainingResult == null) {
			return Result.failure(ResultCode.TRAINING_RESULT_NOT_EXIST);
		} else {
			try {
				trainingResult = trainingResultService.SetTrainingResultState(id, state);
				return Result.success(trainingResult);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	
	@ApiOperation("管理员搜索实训结果列表")
	@RequestMapping(value = "admin/trainingResults/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListTrainingResult(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(trainingResultService.ListTrainingResult(page, count, false));
	}
	
	@ApiOperation("用户搜索实训结果列表")
	@RequestMapping(value = "user/trainingResults/{page}/{count}/{userId}",method = RequestMethod.GET)
	public Result UserListTrainingResult(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@PathVariable("userId")Integer id) {
		return Result.success(trainingResultService.ListTrainingResultWithUser(id, page, count, true));
	}
}

